﻿/// <reference path="//Microsoft.WinJS.1.0/js/base.js" />
/// <reference path="//Microsoft.WinJS.1.0/js/ui.js" />

(function () {
    "use strict";

    window.WinJS.Namespace.define("Dices", {
        d4: window.WinJS.Binding.as(
            {
                name: "d4",
                number: 0,
                getName: function () {
                    return this.name;
                },
                getNumber: function () {
                    return this.number;
                },
                increase: function () {
                    this.number = this.number + 1;
                },
                decrease: function () {
                    if (this.number >= 1) {
                        this.number = this.number - 1;
                    } else {
                        this.number = 0;
                    }
                },
                rollDice: function () {
                    return randomToN(4);
                }
            }
        ),
        d6: window.WinJS.Binding.as(
            {
                name: "d6",
                number: 0,
                getName: function () {
                    return this.name;
                },
                getNumber: function () {
                    return this.number;
                },
                increase: function () {
                    this.number = this.number + 1;
                },
                decrease: function () {
                    if (this.number >= 1) {
                        this.number = this.number - 1;
                    } else {
                        this.number = 0;
                    }
                },
                rollDice: function () {
                    return randomToN(6);
                }
            }
        ),
        d8: window.WinJS.Binding.as(
            {
                name: "d8",
                number: 0,
                getName: function () {
                    return this.name;
                },
                getNumber: function () {
                    return this.number;
                },
                increase: function () {
                    this.number = this.number + 1;
                },
                decrease: function () {
                    if (this.number >= 1) {
                        this.number = this.number - 1;
                    } else {
                        this.number = 0;
                    }
                },
                rollDice: function () {
                    return randomToN(8);
                }
            }
        ),
        d10: window.WinJS.Binding.as(
            {
                name: "d10",
                number: 0,
                getName: function () {
                    return this.name;
                },
                getNumber: function () {
                    return this.number;
                },
                increase: function () {
                    this.number = this.number + 1;
                },
                decrease: function () {
                    if (this.number >= 1) {
                        this.number = this.number - 1;
                    } else {
                        this.number = 0;
                    }
                },
                rollDice: function () {
                    return randomToN(10);
                }
            }
        ),
        dPercent: window.WinJS.Binding.as(
            {
                name: "dPercent",
                number: 0,
                getName: function () {
                    return this.name;
                },
                getNumber: function () {
                    return this.number;
                },
                increase: function () {
                    this.number = this.number + 1;
                },
                decrease: function () {
                    if (this.number >= 1) {
                        this.number = this.number - 1;
                    } else {
                        this.number = 0;
                    }
                },
                rollDice: function () {
                    return ((randomToN(10) - 1) * 10) + randomToN(10);
                }
            }
        ),
        d12: window.WinJS.Binding.as(
            {
                name: "d12",
                number: 0,
                getName: function () {
                    return this.name;
                },
                getNumber: function () {
                    return this.number;
                },
                increase: function () {
                    this.number = this.number + 1;
                },
                decrease: function () {
                    if (this.number >= 1) {
                        this.number = this.number - 1;
                    } else {
                        this.number = 0;
                    }
                },
                rollDice: function () {
                    return randomToN(12);
                }
            }
        ),
        d20: window.WinJS.Binding.as(
            {
                name: "d20",
                number: 0,
                getName: function () {
                    return this.name;
                },
                getNumber: function () {
                    return this.number;
                },
                increase: function () {
                    this.number = this.number + 1;
                },
                decrease: function () {
                    if (this.number >= 1) {
                        this.number = this.number - 1;
                    } else {
                        this.number = 0;
                    }
                },
                rollDice: function () {
                    return randomToN(20);
                }
            }
        ),
    });
})();

//function to get random number from 1 to n
function randomToN(maxVal, floatVal) {
    var randVal = Math.random() * maxVal + 1;
    if (randVal > maxVal) {
        randVal = maxVal;
    }
    return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal);
}